package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.service.IMessageService;
import com.example.forum.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 功能描述: 私信接口
 *
 * @author Lenovo
 * @date 2024/12/27
 */
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private IMessageService messageService;

    @Resource
    private IUserService userService;

    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                          @RequestParam("content") @Validated String content) {
        log.info("--------------------------");
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //用户被禁言
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        if (Objects.equals(user.getId(), receiveUserId)) {
            return AppResult.failed("不能给自己发送私信");
        }
        //校验接收者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getDeleteState() == 1) {
            return AppResult.failed("接受者状态异常");
        }
        //封装对象创建私信
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);
        return AppResult.success();
    }

    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }

    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }

    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request, @RequestParam("id") @NonNull Long id) {
        //根据id查询私信
        Message message = messageService.selectById(id);
        //校验私信状态
        if (message == null || message.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getId() != message.getReceiveUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //更新状态为已读
        messageService.updateStateById(id, (byte) 1);
        return AppResult.success();
    }

    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                             Long repliedId,
                            String content) {
        log.info("=========================");
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //校验用户状态
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询私信
        Message selectMessage = messageService.selectById(repliedId);
        if(selectMessage == null || selectMessage.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //不能回复自己的私信
        if(user.getId() == selectMessage.getPostUserId()){
            return AppResult.failed("不能给自己回复私信");
        }
        //构造对象
        Message message = new Message();
        message.setContent(content);
        message.setPostUserId(user.getId());
        Long receiveUserId = selectMessage.getPostUserId();
        message.setReceiveUserId(receiveUserId);
        //回复私信
        messageService.reply(repliedId, message);
        return AppResult.success();
    }
}
